package com.car.controller;

import com.alipay.api.AlipayApiException;
import com.car.entity.Accidenttable;
import com.car.entity.Lacar;
import com.car.entity.Laorder;
import com.car.entity.User;
import com.car.service.CarrentalorderformService;
import com.car.service.LacarService;
import com.car.service.LaorderService;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/lahuo")
public class LacarController {
	@Autowired
	LacarService lacar;
	@Autowired
	LaorderService laorder;
	@Autowired
	CarrentalorderformService carrentalorderformService;
	
	
@RequestMapping("/tolahuo")
public String tolahuo(Model model) {
	List<Lacar> listall=lacar.selectall();
	model.addAttribute("listall", listall);
	return "H5phone/lahuo";
}

@ResponseBody
@RequestMapping("/carinfo")
public Lacar tocarinfo(int id) {
	Lacar car=lacar.selectlist(id);
		return car;
}

@RequestMapping("/toaddress")
public String toaddress(Model model,int id,String fashou,HttpSession session) {
	model.addAttribute("fashou",fashou);
	session.setAttribute("id", id);
	return "H5phone/lahuoaddresss";
}

@RequestMapping("/tolahuoinfo")
public String tolahuoinfo(Model model,String lnt,String lat, String address,HttpSession session) {
	List<Lacar> listall=lacar.selectall();

	int id=(int) session.getAttribute("id");
	if(id==1) {
		session.setAttribute("lnta", lnt);
		session.setAttribute("lata", lat);
		String lntb= (String) session.getAttribute("lntb");
		String latb=(String) session.getAttribute("latb");
		if(lntb==null&&latb==null) {
			lntb="113.661999";
			latb="34.752655";
			
		}
		
		model.addAttribute("lntone", lnt);
		model.addAttribute("latone", lat);
		model.addAttribute("lnttwo", lntb);
		model.addAttribute("lattwo", latb);
		session.setAttribute("addressa", address);
		String addressb=(String) session.getAttribute("addressb");
		if(addressb==null) {
			addressb="郑州";
		}
		model.addAttribute("addressa", address);
		model.addAttribute("addressb", addressb);
		model.addAttribute("listall", listall);
		return "H5phone/lahuo";
	}else if(id==2) {
		session.setAttribute("lntb", lnt);
		session.setAttribute("latb", lat);
		String lnta= (String) session.getAttribute("lnta");
		String lata=(String) session.getAttribute("lata");
		session.setAttribute("addressb", address);
		if(lnta==null&&lata==null) {
			lnta="112.409451";
			lata="34.662518";
		}
		model.addAttribute("lntone", lnta);
		model.addAttribute("latone", lata);
		model.addAttribute("lnttwo", lnt);
		model.addAttribute("lattwo", lat);
		String addressa=(String) session.getAttribute("addressa");
		if(addressa==null) {
			addressa="洛阳";
		}
		model.addAttribute("addressa", addressa);
		model.addAttribute("addressb", address);
		model.addAttribute("listall", listall);
		return "H5phone/lahuo";
	}return "H5phone/lahuo";
}

@RequestMapping("/tomyorder")
public String tomyorder(Model model,int id,int numbers,int price,String faaddress,String shouaddress,String falnt,String falat,String shoulnt,String shoulat) {
model.addAttribute("id", id);
model.addAttribute("numbers",numbers);
model.addAttribute("price",price);
model.addAttribute("faaddress", faaddress);
model.addAttribute("shouaddress",shouaddress );
model.addAttribute("falnt", falnt);
model.addAttribute("falat", falat);
model.addAttribute("shoulnt",shoulnt);
model.addAttribute("shoulat",shoulat);
	return "H5phone/myorder";	
}

@RequestMapping("/topay")
public String topay(HttpSession session,Model model,String name,String phone,int id,int numbers,int price,String faaddress,String shouaddress,String falnt,String falat,String shoulnt,String shoulat,String data) throws ParseException {
	User user=(User) session.getAttribute("loginSuccess");
	int userid=user.getUserId();
	Random random = new Random();
	String orderid="";
	for (int i=0;i<6;i++)
	{
		orderid+=random.nextInt(10);
	}
	System.out.println(orderid);
	session.setAttribute("orderid", orderid);
	model.addAttribute("id", id);
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
	
	Date date=format.parse(data);
	model.addAttribute("numbers",numbers);
	model.addAttribute("price",price);
	model.addAttribute("faaddress", faaddress);
	model.addAttribute("shouaddress",shouaddress );
	model.addAttribute("falnt", falnt);
	model.addAttribute("falat", falat);
	model.addAttribute("shoulnt",shoulnt);
	model.addAttribute("shoulat",shoulat);
	session.setAttribute("price", price);
	Laorder la=new Laorder();
	la.setCarid(id);
	la.setData(date);
	la.setFaaddress(faaddress);
	la.setFalat(falat);
	la.setFalnt(falnt);
	la.setMoney(price);
	la.setName(name);
	la.setNumber(numbers);
	la.setOrderid(orderid);
	la.setPhone(phone);
	la.setShouaddress(shouaddress);
	la.setShoulat(shoulat);
	la.setShoulnt(shoulnt);
	la.setState(3);
	la.setUserid(userid);
laorder.orderadd(la);
	return "H5phone/lahuopay";
}
//session.getAttribute("ordersIdAddToPay").toString()
@RequestMapping("/ali")
public void ali(HttpServletResponse response, HttpServletRequest request,HttpSession session) throws IOException, AlipayApiException{
	String orderid=session.getAttribute("orderid").toString();
	System.out.println(orderid);
	int price=(int) session.getAttribute("price");
	System.out.println(price);
	
	laorder.ali(response, request,session.getAttribute("orderid").toString(),Integer.parseInt(session.getAttribute("price").toString()));
	
	
}
@RequestMapping("/toail")
public String toali(String orderid,Integer money,HttpSession session) {
session.setAttribute("orderid", orderid);
session.setAttribute("price", money);
	
	return"redirect:ali";
}

/*@RequestMapping("orderInfo")
public String orderinfo(Model model) {
	
	model.addAttribute("orderState", 0);
	return"H5phone/laorder";
}*/

@RequestMapping("/laorderInfo")
public String laorderinfo(@RequestParam(value = "orderState", required = false) Integer orderState,Model model,HttpSession session) {
	if(orderState==null) {
		orderState=0;
	}
	User user=(User) session.getAttribute("loginSuccess");
	List<Laorder> orderlist=laorder.selectlist(orderState, user.getUserId());
	List<Lacar> car=lacar.selectall();
	
	int counts=laorder.counts(orderState, user.getUserId());

	model.addAttribute("orderState", orderState);
	model.addAttribute("orderlist", orderlist);
	model.addAttribute("car", car);
	model.addAttribute("count", counts);
	return"H5phone/laorder";
}
@RequestMapping("topays")
public String topay(HttpSession session) {
	String order=(String) session.getAttribute("orderid");
	laorder.stateupd(order);
	return "H5phone/test";
}
  
}